//package GUI;
//
////package manager;
//
//import student.Student;
//
//import javax.swing.*;
//import javax.swing.table.DefaultTableModel;
//import java.awt.*;
//import java.awt.event.ActionEvent;
//import java.io.*;
//import java.util.ArrayList;
//import java.util.List;
//
//public class StudentManagerGUI {
//    private List<Student> students;
//    private DefaultTableModel tableModel;
//    private JFrame frame;
//
//    public StudentManagerGUI() {
//        students = new ArrayList<>();
//        initializeGUI();
//    }
//
//    private void initializeGUI() {
//        frame = new JFrame("Student Management System");
//        frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
//        frame.setSize(800, 500);
//        frame.setLayout(new BorderLayout());
//
//        // 表格部分
//        String[] columnNames = {"ID", "Name", "Gender", "Age", "Grade"};
//        tableModel = new DefaultTableModel(columnNames, 0);
//        JTable table = new JTable(tableModel);
//        JScrollPane scrollPane = new JScrollPane(table);
//
//        // 按钮面板
//        JPanel buttonPanel = new JPanel();
//        buttonPanel.setLayout(new FlowLayout());
//
//        JButton addButton = new JButton("Add");
//        JButton deleteButton = new JButton("Delete");
//        JButton updateButton = new JButton("Update");
//        JButton saveButton = new JButton("Save");
//        JButton loadButton = new JButton("Load");
//        JButton statsButton = new JButton("Statistics");
//
//        buttonPanel.add(addButton);
//        buttonPanel.add(deleteButton);
//        buttonPanel.add(updateButton);
//        buttonPanel.add(saveButton);
//        buttonPanel.add(loadButton);
//        buttonPanel.add(statsButton);
//
//        frame.add(scrollPane, BorderLayout.CENTER);
//        frame.add(buttonPanel, BorderLayout.SOUTH);
//
//        // 事件绑定
//        addButton.addActionListener(e -> addStudent());
//        deleteButton.addActionListener(e -> deleteStudent(table));
//        updateButton.addActionListener(e -> updateStudent(table));
//        saveButton.addActionListener(e -> saveToFile());
//        loadButton.addActionListener(e -> loadFromFile());
//        statsButton.addActionListener(e -> classStatistics());
//
//        frame.setVisible(true);
//    }
//
//    // 添加学生
//    private void addStudent() {
//        JTextField idField = new JTextField();
//        JTextField nameField = new JTextField();
//        JTextField genderField = new JTextField();
//        JTextField ageField = new JTextField();
//        JTextField gradeField = new JTextField();
//
//        Object[] fields = {
//                "ID:", idField,
//                "Name:", nameField,
//                "Gender:", genderField,
//                "Age:", ageField,
//                "Grade:", gradeField
//        };
//
//        int result = JOptionPane.showConfirmDialog(frame, fields, "Add Student", JOptionPane.OK_CANCEL_OPTION);
//        if (result == JOptionPane.OK_OPTION) {
//            String id = idField.getText();
//            String name = nameField.getText();
//            String gender = genderField.getText();
//            int age = Integer.parseInt(ageField.getText());
//            double grade = Double.parseDouble(gradeField.getText());
//
//            Student student = new Student(id, name, gender, age, grade);
//            students.add(student);
//            tableModel.addRow(new Object[]{id, name, gender, age, grade});
//        }
//    }
//
//    // 删除学生
//    private void deleteStudent(JTable table) {
//        int selectedRow = table.getSelectedRow();
//        if (selectedRow >= 0) {
//            students.remove(selectedRow);
//            tableModel.removeRow(selectedRow);
//            JOptionPane.showMessageDialog(frame, "Student deleted.");
//        } else {
//            JOptionPane.showMessageDialog(frame, "Please select a row to delete.", "Error", JOptionPane.ERROR_MESSAGE);
//        }
//    }
//
//    // 修改学生
//    private void updateStudent(JTable table) {
//        int selectedRow = table.getSelectedRow();
//        if (selectedRow >= 0) {
//            Student student = students.get(selectedRow);
//
//            JTextField nameField = new JTextField(student.getName());
//            JTextField genderField = new JTextField(student.getGender());
//            JTextField ageField = new JTextField(String.valueOf(student.getAge()));
//            JTextField gradeField = new JTextField(String.valueOf(student.getGrade()));
//
//            Object[] fields = {
//                    "Name:", nameField,
//                    "Gender:", genderField,
//                    "Age:", ageField,
//                    "Grade:", gradeField
//            };
//
//            int result = JOptionPane.showConfirmDialog(frame, fields, "Update Student", JOptionPane.OK_CANCEL_OPTION);
//            if (result == JOptionPane.OK_OPTION) {
//                student.setName(nameField.getText());
//                student.setGender(genderField.getText());
//                student.setAge(Integer.parseInt(ageField.getText()));
//                student.setGrade(Double.parseDouble(gradeField.getText()));
//
//                tableModel.setValueAt(student.getName(), selectedRow, 1);
//                tableModel.setValueAt(student.getGender(), selectedRow, 2);
//                tableModel.setValueAt(student.getAge(), selectedRow, 3);
//                tableModel.setValueAt(student.getGrade(), selectedRow, 4);
//
//                JOptionPane.showMessageDialog(frame, "Student updated.");
//            }
//        } else {
//            JOptionPane.showMessageDialog(frame, "Please select a row to update.", "Error", JOptionPane.ERROR_MESSAGE);
//        }
//    }
//
//    // 保存到文件
//    private void saveToFile() {
//        try (ObjectOutputStream oos = new ObjectOutputStream(new FileOutputStream("students.dat"))) {
//            oos.writeObject(students);
//            JOptionPane.showMessageDialog(frame, "Students saved to file.");
//        } catch (IOException e) {
//            JOptionPane.showMessageDialog(frame, "Error saving to file: " + e.getMessage(), "Error", JOptionPane.ERROR_MESSAGE);
//        }
//    }
//
//    // 从文件加载
//    private void loadFromFile() {
//        try (ObjectInputStream ois = new ObjectInputStream(new FileInputStream("students.dat"))) {
//            students = (List<Student>) ois.readObject();
//            tableModel.setRowCount(0); // 清空表格
//            for (Student student : students) {
//                tableModel.addRow(new Object[]{student.getId(), student.getName(), student.getGender(), student.getAge(), student.getGrade()});
//            }
//            JOptionPane.showMessageDialog(frame, "Students loaded from file.");
//        } catch (IOException | ClassNotFoundException e) {
//            JOptionPane.showMessageDialog(frame, "Error loading from file: " + e.getMessage(), "Error", JOptionPane.ERROR_MESSAGE);
//        }
//    }
//
//    // 班级统计
//    private void classStatistics() {
//        long totalStudents = students.size();
//        long males = students.stream().filter(s -> s.getGender().equalsIgnoreCase("Male")).count();
//        long females = totalStudents - males;
//        double averageGrade = students.stream().mapToDouble(Student::getGrade).average().orElse(0);
//
//        JOptionPane.showMessageDialog(frame,
//                "Total Students: " + totalStudents + "\n" +
//                        "Male Students: " + males + "\n" +
//                        "Female Students: " + females + "\n" +
//                        "Average Grade: " + averageGrade,
//                "Class Statistics", JOptionPane.INFORMATION_MESSAGE);
//    }
//
//    public static void main(String[] args) {
//        new StudentManagerGUI();
//    }
//}
